<?php

namespace app\admin\controller;

use app\admin\logic\CompanyLogic;
use app\admin\logic\CompanyUserLogic;
use app\admin\validate\CompanyValidate;
use support\Request;
use support\Response;

/**
 * 合作企业控制器
 */
class CompanyController extends BaseController
{
    /**
     * 构造
     */
    public function __construct()
    {
        $this->logic = new CompanyLogic();
        $this->validate = new CompanyValidate();
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request) : Response
    {
        $where = $request->more([
            ['name', ''],
            ['short_name', ''],
            ['contact', ''],
            ['phone', ''],
            ['type', ''],
            ['platform_type', ''],
            ['status', ''],
            ['create_time', []],
            ['business_manager', ''],
            ['operation_manager', ''],
            ['project_manager', ''],
        ]);
        $query = $this->logic->search($where)->with(['business', 'operation', 'project'])->field('*, area_id as area');
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 修改状态
     * @param Request $request
     * @return Response
     */
    public function changeStatus(Request $request) : Response
    {
        $id = $request->input('id', '');
        $status = $request->input('status', 1);
        $model = $this->logic->findOrEmpty($id);
        if ($model->isEmpty()) {
            return $this->fail('未查找到信息');
        }
        $result = $model->save(['status' => $status]);
        if ($result) {
            $this->afterChange('changeStatus', $model);
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
    

    public function companyAccountList(Request $request) : Response
    {
        $companyId = $request->input('company_id', 0);
        $logic = new CompanyUserLogic();
        $data = $logic->getList($companyId);
        return $this->success($data);
    }
    

    public function companyAccountSave(Request $request)
    {
        $params = $request->more([
            ['company_id', 0],
            ['username', ''],
            ['password', ''],
            ['head_img', ''],
            ['phone', ''],
            ['status', ''],
            ['email', ''],
            ['role_ids', []],
        ]);
        $logic = new CompanyUserLogic();
        $result = $logic->saveCompanyUser($params);
        return $result === false ? $this->fail('操作失败') : $this->success('操作成功');
    }

    public function companyAccountUpdate(Request $request)
    {
        $params = $request->more([
            ['company_id', 0],
            ['username', ''],
            ['password', ''],
            ['head_img', ''],
            ['phone', ''],
            ['status', ''],
            ['email', ''],
            ['role_ids', []],
        ]);
        $id = $request->input('id', 0);
        $logic = new CompanyUserLogic();
        $result = $logic->updateCompanyUser($params, $id);
        return $result === false ? $this->fail('操作失败') : $this->success('操作成功');
    }

    public function companyAccountDelete(Request $request)
    {
        $ids = $request->input('ids', []);
        $logic = new CompanyUserLogic();
        $result = $logic->deleteCompanyUser($ids);
        return $result === false ? $this->fail('操作失败') : $this->success('操作成功');
    }
}
